package com.woniuxy.kaer.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.kaer.service.entity.ServiceOrder;
import com.woniuxy.kaer.service.entity.ServiceReal;
import com.woniuxy.kaer.service.entity.ServiceRealDetail;
import com.woniuxy.kaer.service.mapper.ServiceOrderMapper;
import com.woniuxy.kaer.service.mapper.ServiceRealDetailMapper;
import com.woniuxy.kaer.service.mapper.ServiceRealMapper;
import com.woniuxy.kaer.service.param.AddServiceRealDetailParam;
import com.woniuxy.kaer.service.param.UpdateRealDetailParam;
import com.woniuxy.kaer.service.service.ServiceRealDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 程诗洋
 * @since 2025年02月17日
 */
@Service
public class ServiceRealDetailServiceImpl extends ServiceImpl<ServiceRealDetailMapper, ServiceRealDetail> implements ServiceRealDetailService {

    @Resource
    private ServiceRealMapper serviceRealMapper;
    @Resource
    private ServiceRealDetailMapper serviceRealDetailMapper;
    @Resource
    private ServiceOrderMapper serviceOrderMapper;

    @Override
    public void updateServiceRealDetail(UpdateRealDetailParam param) throws Exception {
        ServiceRealDetail serviceRealDetail = serviceRealDetailMapper.selectById(param.getRealDetailId());
        if(null==serviceRealDetail){
            throw new Exception("订单消耗品不存在");
        }
        if(param.getNumber()==0){
            serviceRealDetailMapper.deleteById(serviceRealDetail);
        }else{
            serviceRealDetail.setMaterialNum(param.getNumber());
            serviceRealDetailMapper.updateById(serviceRealDetail);
        }

    }

    @Override
    public void deleteServiceRealDetail(String serviceOrderId,Integer serviceRealDetailId, Integer founderId) throws Exception {
        ServiceRealDetail serviceRealDetail = serviceRealDetailMapper.selectById(serviceRealDetailId);
        if(null==serviceRealDetail){
            throw new Exception("订单消耗品不存在");
        }
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(serviceOrderId);
        serviceOrder.setTotalPrice(serviceOrder.getTotalPrice().subtract(serviceRealDetail.getMaterialPrice()));
        serviceOrderMapper.updateById(serviceOrder);
        serviceRealDetailMapper.deleteById(serviceRealDetail);
    }

    @Override
    public void realDetailByIncrease(Integer serviceRealDetailId, Integer founderId) throws Exception {
        ServiceRealDetail serviceRealDetail = serviceRealDetailMapper.selectById(serviceRealDetailId);
        if(null==serviceRealDetail){
            throw new Exception("订单消耗品不存在");
        }
        serviceRealDetail.setMaterialNum(serviceRealDetail.getMaterialNum()+1);
        serviceRealDetailMapper.updateById(serviceRealDetail);
    }

    @Override
    public void realDetailByDecreaseOne(Integer serviceRealDetailId, Integer founderId) throws Exception {
        ServiceRealDetail serviceRealDetail = serviceRealDetailMapper.selectById(serviceRealDetailId);
        if(null==serviceRealDetail){
            throw new Exception("订单消耗品不存在");
        }
        if(serviceRealDetail.getMaterialNum()==1){
            serviceRealDetailMapper.deleteById(serviceRealDetail);
        }else {
            serviceRealDetail.setMaterialNum(serviceRealDetail.getMaterialNum()-1);
            serviceRealDetailMapper.updateById(serviceRealDetail);
        }

    }

    @Override
    public void addServiceRealDetail(AddServiceRealDetailParam param) throws Exception {
        ServiceReal serviceReal = serviceRealMapper.selectById(param.getServiceRealId());
        if (null==serviceReal){
            throw new Exception("服务订单项目不存在");
        }
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(serviceReal.getServiceOrderId());
        if(null==serviceOrder){
            throw new Exception("服务订单不存在");
        }
        ServiceRealDetail serviceRealDetail = BeanUtil.toBean(param, ServiceRealDetail.class);
        serviceRealDetail.setServiceRealId(serviceReal.getServiceRealId());
        serviceOrder.setTotalPrice(serviceOrder.getTotalPrice().add(serviceRealDetail.getMaterialPrice()));
        serviceOrderMapper.updateById(serviceOrder);

    }
}
